//
//  ViewController.m
//  WangI
//
//  Created by wjn on 15/11/23.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "ViewController.h"
#import "NewsModel.h"
#import "ScrollWithImage.h"
#import "UIImageView+WebCache.h"
#import "NewsTableViewCell.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate>
@property (nonatomic, retain)NSMutableArray *dataSource;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UIScrollView *scrollView;
@end

@implementation ViewController


- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self getData];
    self.navigationController.navigationBarHidden = NO;
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"网易新闻";
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
   // [self.view addSubview:self.tableView];
}
-(void)getData {
    [SANetWorkingTask request:kNewsGETURL parmater:nil block:^(id result) {
        NSArray *array = result[@"T1348647909107"];
        for (NSDictionary *dic in array) {
            NewsModel *newsModel = [[NewsModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:newsModel];
        }
        [self.collectionView reloadData];
    }];
}
#pragma mark - 懒加载
-(NSMutableArray *)dataSource {
    if (!_dataSource) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height -100);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.pagingEnabled = YES;
        self.collectionView.bounces = NO;
        self.collectionView.delegate = self;
        self.collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(-100, 0, 607, 0);
        [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"hehe"];
        [self.collectionView registerClass:[ITem2CollectionViewCell class] forCellWithReuseIdentifier:@"haha"];
        [self.collectionView registerClass:[ITem3CollectionViewCell class] forCellWithReuseIdentifier:@"xixi"];
        [self.collectionView registerClass:[ITem4CollectionViewCell class] forCellWithReuseIdentifier:@"wuwu"];
        [self.collectionView registerClass:[ITem5CollectionViewCell class] forCellWithReuseIdentifier:@"youyou"];
    }
    return _collectionView;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hehe" forIndexPath:indexPath];
        cell.tag = 1000;
       return cell;
    } else if(indexPath.item == 1){
        ITem2CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"haha" forIndexPath:indexPath];
        cell.tag = 1001;
        return cell;
       
    } else if(indexPath.item == 2){
        ITem3CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"xixi" forIndexPath:indexPath];
        cell.tag = 1002;
        return cell;
    } else if(indexPath.item == 3){
        ITem4CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wuwu" forIndexPath:indexPath];
        cell.tag = 1003;
        return cell;
    } else if (indexPath.item == 4){
        ITem5CollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"youyou" forIndexPath:indexPath];
        cell.tag = 1004;
        return cell;
    }
    return nil;
}
-(UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 50)];
        NSArray *array = [NSArray arrayWithObjects:@"头条", @"娱乐", @"体育", @"财经", @"科技", nil];
        for (int i = 0; i < 5; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(i * self.view.frame.size.width / 5, 0, self.view.frame.size.width / 5, 50);
            [button setTitle:array[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:
              UIControlStateNormal];
            button.tag = 1000 + i;
            button.backgroundColor = [UIColor whiteColor];
            [button addTarget:self action:@selector(pressDidButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];
            if (button.tag == 1000) {
                [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:20];
            }
        }
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width / 5 * 5, 0);
        //self.scrollView.showsHorizontalScrollIndicator = YES;
        
    }
    return _scrollView;
}
- (void)pressDidButton:(UIButton *)sender {
    UIButton *button = (UIButton *)[self.scrollView viewWithTag:sender.tag];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.collectionView setContentOffset:CGPointMake((sender.tag - 1000) * self.view.frame.size.width, -65) animated:YES];
    
    for (int i = 0; i < 5; i++) {
        if (sender.tag == 1000 + i) {
            continue;
        }else {
            UIButton *button = (UIButton *)[self.scrollView viewWithTag:1000 + i];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        UIButton *button = (UIButton *)[self.scrollView viewWithTag:1000 + scrollView.contentOffset.x / scrollView.frame.size.width];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    UIButton *button1 = (UIButton *)[self.scrollView viewWithTag:999 +scrollView.contentOffset.x / scrollView.frame.size.width];
    button1.titleLabel.font = [UIFont systemFontOfSize:15];
     [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIButton *button2 = (UIButton *)[self.scrollView viewWithTag:scrollView.contentOffset.x / scrollView.frame.size.width + 1001];
     [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button2.titleLabel.font = [UIFont systemFontOfSize:15];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
